package  com.makinolo.rlvforge
{
	import com.google.maps.*;
	import com.gskinner.motion.GTweener;
	
	import flash.display.*;
	import flash.events.*;
	
	import mx.containers.Canvas;
	import mx.controls.Button;
	import mx.core.UIComponent;
	
	[Event (name="MapMenuItemClicked", type="flash.events.Event")]
	
	public class MapMenu extends UIComponent
	{
		private var menuOptionCount:int = 0; 
		
		private var menuBalls:Array;
		private var canvas:Canvas;
		private var location:LatLng;
		private var selectedOption:Number;
		private var defaultOption:Number;
			
		public function MapMenu(pos:LatLng, menuopts:Array, defaultopt:int)
		{
			super();
			var gr:Graphics;
			location = pos;
			menuOptionCount = menuopts.length;	
			defaultOption = defaultopt;		
			canvas = new Canvas();
			canvas.height = 20 * menuOptionCount;
			canvas.width = 50;
			canvas.y = -canvas.height;
			canvas.visible = false;
    		addChild(canvas);
			menuBalls = new Array(menuOptionCount);
			for (var i:int=0; i<menuOptionCount; i++)
			{				
				menuBalls[i] = new Button();
				menuBalls[i].styleName = "MapMenuButton";	
				menuBalls[i].width = 50;
				menuBalls[i].height= 20;
				menuBalls[i].y = canvas.height - menuBalls[i].height;								
				menuBalls[i].label = menuopts[i];						
				menuBalls[i].name = i;
				menuBalls[i].addEventListener(MouseEvent.CLICK, menuOptionClicked);
				canvas.addChild(menuBalls[i]);     					    				
			}						
			
			var sprite:Sprite = new Sprite();			
			sprite.graphics.beginFill(0xeeee00);
			sprite.graphics.lineStyle(2, 0x0);
			sprite.graphics.drawRect(0,0,7,7);
			sprite.graphics.endFill();
			sprite.x = -3;
			sprite.y = -3;
			addChild(sprite);
			
			sprite.addEventListener(MouseEvent.CLICK, openMenu);
			this.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
			this.setFocus();
		}
		
		
		private function openMenu(e:MouseEvent):void
		{
			var segmentsize:Number = (Math.PI*2) / menuOptionCount;
			var targety:Number;
			canvas.visible = true;
			for (var i:int=0; i<menuOptionCount; i++)
			{
				targety = menuBalls[i].height * ( menuBalls.length - (i+1))
				GTweener.to(menuBalls[i], 0.3, {x:0, y:targety, alpha:1}, {repeatCount:1});				
			}
			e.currentTarget.removeEventListener(MouseEvent.CLICK, openMenu);			
			e.currentTarget.addEventListener(MouseEvent.CLICK, closeMenu);
		}
		
		private function closeMenu(e:MouseEvent):void
		{			
			for (var i:int=0; i<menuOptionCount; i++)
			{								
				GTweener.to(menuBalls[i], 0.3, {x:0, y:canvas.height - menuBalls[i].height, alpha:0}, {repeatCount:1});					
			}			
			e.currentTarget.removeEventListener(MouseEvent.CLICK, closeMenu);
			e.currentTarget.addEventListener(MouseEvent.CLICK, openMenu);
		}
		
		public function getLocation():LatLng
		{
			return location;
		}
		
		public function getSelectedOption():Number
		{
			return selectedOption;
		}
		
		private function menuOptionClicked(e:MouseEvent):void
		{			
			selectedOption = int(e.currentTarget.name);
			var evt:Event = new Event("MapMenuItemClicked");
			this.dispatchEvent(evt);						
		}
		
		private function keyPressed(e:KeyboardEvent):void
		{	
			for (var i:int=0; i<menuOptionCount; i++)
			{					
				if (e.charCode == menuBalls[i].label.toLowerCase().charCodeAt(0) )
				{
					selectedOption = int(menuBalls[i].name);
					var evt:Event = new Event("MapMenuItemClicked");
					this.dispatchEvent(evt);
				}
			}
		}
		

	}
}